<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

namespace data\formats;

defined('ANS') or die();

class Codefile extends Formats implements iFormats {
	var $format = 'codefile';
	var $code = '';

	public function check ($value) {
		$this->error = array();

		if (is_array($value['']) && $value['']['filename']) {
			return $this->validate($value['']['filename']);
		}

		return true;
	}

	public function valueForm ($value) {
		$file = $this->settings['']['base_path'].$this->settings['']['uploads'].$value[''];

		return array(
			'filename' => preg_replace('|^'.preg_quote($this->settings['']['subfolder'], '|').'|', '', $value['']),
			'code' => is_file($file) ? file_get_contents($file) : ''
		);
	}

	public function valueDB (\data\Data $Data, $value, $language = '', $id = 0) {
		$filepath = $this->settings['']['base_path'].$this->settings['']['uploads'];

		$code = $value['code'] ? $value['code'] : $value[''];
		$filename = $value['filename'];

		if ($id && !$filename) {
			$field = $this->getField('', $language);

			$filename = $Data->select(array(
				'table' => $this->table,
				'fields' => $field,
				'conditions' => array(
					'id' => $id
				),
				'limit' => 1,
				'comment' => __('Selecting the filename of %s', $this->name)
			));

			$filename = $filename[$field];
		}

		$filename = $this->settings['']['subfolder'].($filename ? preg_replace('|^'.preg_quote($this->settings['']['subfolder'], '|').'|', '', $filename) : uniqid().'.php');

		$File = new \files\File();

		$File->saveText($code, $filepath.$filename);

		return array('' => $filename);
	}

	public function afterSave (\data\Data $Data, $values) {
		if ($values['old_value'][''] && ($values['old_value'][''] != $values['new_value'][''])) {
			$old_file = $this->settings['']['base_path'].$this->settings['']['uploads'].$values['old_value'][''];

			if (is_file($old_file)) {
				unlink($old_file);
			}
		}

		return true;
	}

	public function settings ($settings) {
		global $Config;

		$this->bindEvent(array('afterUpdate', 'afterDelete'), array($this, 'afterSave'));

		$this->settings = $this->setSettings($settings, array(
			'' => array(
				'db_type' => 'varchar',

				'length_max' => 150,
				'base_path' => SCENE_PATH,
				'uploads' => $Config->scene_paths['uploads'],
				'subfolder' => $this->table.'/'.$this->name.'/'
			)
		));

		$this->subformats[] = 'filename';
		$this->subformats[] = 'code';

		return $this->settings;
	}
}
?>
